package com.kust.smarhome.background.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kust.smarhome.background.model.User;
import com.kust.smarhome.background.service.IMenuService;
import com.kust.smarhome.background.service.IUserService;
import com.kust.smarhome.background.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class BaseController {

    @Autowired(required = false)
    HttpServletRequest req;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    IUserService userService;

    @Autowired
    IMenuService menuService;


    /**
     * 获取页面
     * @return
     */
    public Page getPage() {
        int current = ServletRequestUtils.getIntParameter(req, "current", 1);
        int size = ServletRequestUtils.getIntParameter(req, "size", 10);

        return new Page(current, size);
    }

    public Map<String, Integer> getInfo() {
        int current = ServletRequestUtils.getIntParameter(req, "current", 1);
        int size = ServletRequestUtils.getIntParameter(req, "size", 10);
        int state = ServletRequestUtils.getIntParameter(req, "type", 10);

        Map<String, Integer> map = new HashMap<>();
        map.put("pageNum", current);
        map.put("pageSize", size);
        map.put("state", state);

        System.out.println(map);

        return map;
    }

    protected boolean getUserPower(String username) {

        User user = userService.getByUsername(username);
        return user.getPower() != 1;
    }
}
